﻿namespace ModuleOne
{
    using System;
    using System.Windows;

    using MEF_Test.Bricks;
    using MEF_Test.Bricks.TemplateProvider;
    using MEF_Test.Contracts;

    public class ModuleOneBrowserViewModel : DependencyObject
    {
        /// <summary>
        /// Gets or sets the browser node template selector.
        /// </summary>
        /// <value>The browser node template selector.</value>
        public BrowserNodeTemplateSelector BrowserNodeTemplateSelector { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ModuleOneBrowserViewModel"/> class.
        /// </summary>
        /// <param name="fileHandlerFactory">The file handler factory.</param>
        /// <param name="mediaHandlerFactory">The media handler factory.</param>
        /// <param name="templateProvider">The template provider.</param>
        /// <param name="localDiskBrowserNodeViewModelFactory">The <see cref="LocalDiskBrowserNodeViewModelFactory"/> factory.</param>
        /// <param name="browserNodeTemplateSelector">The browser node template selector.</param>
        public ModuleOneBrowserViewModel(IDiskFileHandlerFactory fileHandlerFactory, IMediaHandlerFactoryProvider mediaHandlerFactory, TemplateProvider templateProvider, IDiskBrowserNodeViewModelFactory localDiskBrowserNodeViewModelFactory, BrowserNodeTemplateSelector browserNodeTemplateSelector)
        {
            BrowserNodeTemplateSelector = browserNodeTemplateSelector;
            
            if (fileHandlerFactory == null)
            {
                throw new ArgumentNullException("fileHandlerFactory");
            }

            if (mediaHandlerFactory == null)
            {
                throw new ArgumentNullException("mediaHandlerFactory");
            }

            if (templateProvider == null)
            {
                throw new ArgumentNullException("templateProvider");
            }

            if (localDiskBrowserNodeViewModelFactory == null)
            {
                throw new ArgumentNullException("localDiskBrowserNodeViewModelFactory");
            }

            if (browserNodeTemplateSelector == null)
            {
                throw new ArgumentNullException("browserNodeTemplateSelector");
            }

            // Todo : don't pass elem, let it be built inside the browsingprovider. if the root element is needed, implement a GetRootElement method.

            // Create a proper FileHandler
            IDiskFileHandler fileHandler = fileHandlerFactory.Create(@"C:\", "drive/localHardDrive");
            
            // Create a root element to start browsing from.
            var elem = localDiskBrowserNodeViewModelFactory.Create(fileHandler, mediaHandlerFactory, templateProvider);
            BrowsingProvider = new DiskMediaBrowsingProvider(elem, fileHandlerFactory, mediaHandlerFactory, templateProvider, localDiskBrowserNodeViewModelFactory);           
        }
       
        // Todo : change DP to a Notifying .Net Property
        public DiskMediaBrowsingProvider BrowsingProvider
        {
            get { return (DiskMediaBrowsingProvider)GetValue(BrowsingProviderProperty); }
            set { SetValue(BrowsingProviderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BrowsingProvider.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BrowsingProviderProperty =
            DependencyProperty.Register("BrowsingProvider", typeof(DiskMediaBrowsingProvider), typeof(ModuleOneBrowserViewModel), new UIPropertyMetadata(null));     


    }
}
